package com.iman.locker.view.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.iman.locker.R;


/**
 * 数字键盘
 * Created by esirong on 2015/11/13.
 */
public class NumericKeyboard extends LinearLayout implements View.OnClickListener {
    private static final String TAG = "NumericKeyboard";
    private Button num1View;
    private Button num2View;
    private Button num3View;
    private Button num4View;
    private Button num5View;
    private Button num6View;
    private Button num7View;
    private Button num8View;
    private Button num9View;
    private Button num0View;

    /**
     * 结果回调监听器接口
     */
    private CallBack callBack;

    public NumericKeyboard(Context context) {
        this(context, null);
    }

    public NumericKeyboard(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumericKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public NumericKeyboard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View.inflate(context, R.layout.view_number_keyboard, this);
        num1View = (Button) findViewById(R.id.num1);
        num2View = (Button) findViewById(R.id.num2);
        num3View = (Button) findViewById(R.id.num3);
        num4View = (Button) findViewById(R.id.num4);
        num5View = (Button) findViewById(R.id.num5);
        num6View = (Button) findViewById(R.id.num6);
        num7View = (Button) findViewById(R.id.num7);
        num8View = (Button) findViewById(R.id.num8);
        num9View = (Button) findViewById(R.id.num9);
        num0View = (Button) findViewById(R.id.num0);

        num0View.setOnClickListener(this);
        num1View.setOnClickListener(this);
        num2View.setOnClickListener(this);
        num3View.setOnClickListener(this);
        num4View.setOnClickListener(this);
        num5View.setOnClickListener(this);
        num6View.setOnClickListener(this);
        num7View.setOnClickListener(this);
        num8View.setOnClickListener(this);
        num9View.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.num0:
                InputPassword(0);
                break;
            case R.id.num1:
                InputPassword(1);
                break;
            case R.id.num2:
                InputPassword(2);
                break;
            case R.id.num3:
                InputPassword(3);
                break;
            case R.id.num4:
                InputPassword(4);
                break;
            case R.id.num5:
                InputPassword(5);
                break;
            case R.id.num6:
                InputPassword(6);
                break;
            case R.id.num7:
                InputPassword(7);
                break;
            case R.id.num8:
                InputPassword(8);
                break;
            case R.id.num9:
                InputPassword(9);
                break;
        }
    }

    private void InputPassword(int number) {
        Log.d(TAG, "Input:" + number);
        if (callBack != null) {
            callBack.onInput(number);
        }
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onInput(int number);
    }
}
